#include "Game.h"
#include "Scenes/SceneManager.h"
#include "Context.h"
#include "StarEngine.h"
#include "Helpers/Helpers.h"
#include "Assets/FontManager.h"
#include "scenes/MenuScene.h"
#include "scenes/MainScene.h"
#include "Input/InputManager.h"
#include "Graphics/GraphicsManager.h"
#include "Graphics/ScaleSystem.h"
#include "Objects/GlobalVariables.h"

Game::Game()
	: star::BaseGame()
{
	star::StarEngine::GetInstance();
}

void Game::Initialize(int32 window_width, int32 window_height)
{
	star::ScaleSystem::GetInstance()->SetWorkingResolution(160,144);
	star::StarEngine::GetInstance()->Initialize(window_width, window_height), _T("StarEngine couldn't be initialized!");

#ifdef _WIN32
	star::FontManager::GetInstance();
#endif

	MenuScene* menuScene = new MenuScene(_T("MenuScene"));
	star::SceneManager::GetInstance()->AddScene(menuScene->GetName(), menuScene);


	MainScene* mainScene = new MainScene(_T("MainScene"));
	star::SceneManager::GetInstance()->AddScene(mainScene->GetName(), mainScene);

		star::SceneManager::GetInstance()->SetActiveScene(_T("MenuScene"));
	star::Logger::GetInstance()->Log(star::LogLevel::Info, _T("Scenes Set and Done"));

}

void Game::Update(const star::Context& context)
{
	tstring subTitle(_T("... FPS: "));
	subTitle += star::string_cast<tstring>(star::StarEngine::GetInstance()->GetCurrentFPS());
	//star::StarEngine::GetInstance()->SetGameSubTitle(subTitle);


	tstringstream strstr;
	if(star::InputManager::GetInstance()->IsFingerReleasedCP(0))
	{
		auto pos = star::InputManager::GetInstance()->GetCurrentFingerPosCP(0);
		strstr << _T("[") << pos.x << _T("x") << pos.y;
		strstr << _T("] -> [") << star::GraphicsManager::GetInstance()->GetWindowWidth()
			<< _T("x") << star::GraphicsManager::GetInstance()->GetWindowHeight()
			<< _T("]");

		star::Logger::GetInstance()->Log(star::LogLevel::Info, strstr.str());
	}

	return star::StarEngine::GetInstance()->Update(context);
}

void Game::Draw()
{
	return star::StarEngine::GetInstance()->Draw();
}

void Game::End()
{
#ifdef _WIN32
	star::FontManager::GetInstance()->EraseFonts();
#endif
	star::Logger::GetInstance()->Log(star::LogLevel::Info, _T("Game ends."));
	delete GlobalVariables::GetInstance();
	return star::StarEngine::GetInstance()->End();
}
